var app = getApp();
var request = app.request;
var common = require('../../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindMobile: '', //手机号
    bindCode: '', //验证码
    agree: false, //是否同意
    login_flag: 0, //是否可登录
    close: false, //关闭按钮是否显示
    setcode: false, //发送验证码
    countDownNum: '60', //倒计时初始值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.getConfig(function(res) {
      var sms_time_out = common.getConfigByName(res.config, 'sms_time_out');
      if (sms_time_out > 0) {
        that.setData({countDownNum:sms_time_out})
      }
    });
  },
  // 监听手机号输入
  setMobile: function (e) {
    this.data.bindMobile = e.detail.value;
    if (this.data.bindMobile.length > 0) {
      this.setData({
        close: true
      })
    } else {
      this.setData({
        close: false
      })
    }
    this.login_flag();
  },
  //清空手机号
  close: function () {
    this.setData({
      bindMobile: " ",
      close: false,
      login_flag: 0
    })
  },
  // 监听验证码输入
  setCode: function (e) {
    this.data.bindCode = e.detail.value;
    this.login_flag();
  },
  // 登录是否可点击
  login_flag: function () {
    if (this.data.bindMobile.length > 0 && this.data.bindCode.length > 0 && this.data.agree == true) {
      this.setData({
        login_flag: 1
      })
    } else {
      this.setData({
        login_flag: 0
      })
    }
  },
  // 发送验证码
  getCode: function (e) { 
    var that = this;
    if (that.data.bindMobile == '') {
      app.showTextWarining("请输入手机号码");
      return false;
    }
    console.log(that.data.bindMobile.length)
    if (that.data.bindMobile.length != 11) {
      app.showTextWarining("请输入正确的手机号码");
      return false;
    }
    common.sendSmsCode(that.data.bindMobile, null, function (res) {
      if (res.data.status == 1) {
        that.setData({
          setcode: true
        })
        that.countDown();
      }
    });
  },

  //登录注册
  login: function () {
    var that = this;
    if(that.data.bindMobile == ''){
      app.showTextWarining("请输入手机号码！");
      return false;
    }
    if(that.data.bindCode == ''){
      app.showTextWarining("请输入短信验证码！");
      return false;
    }
    if(that.data.bindCode.length !=4){
      app.showTextWarining("验证码错误！");
      return false;
    }
    if(that.data.agree !=true){
      app.showTextWarining("未同意注册协议！");
      return false;
    }
    request.post('/api/user/login_mobile', {
      data: {
        mobile: that.data.bindMobile,
        code:that.data.bindCode,
      },
      success: function (res) {
        app.showSuccess('登录成功', function () {
          wx.setStorageSync('app:userInfo', res.data.result);
          wx.setStorageSync('isAuth', true);
          app.globalData.userInfo = res.data.result;
          app.globalData.wechatUser = res.data.result;
          app.globalData.userInfo.head_pic = common.getFullUrl(app.globalData.userInfo.head_pic);
          wx.removeStorageSync('bind_third_login');
          wx.navigateBack({delta:2});
        });
      }
    });
  },

  // 是否同意协议
  agree: function () {
    var that = this;
    this.setData({
      agree: !that.data.agree
    })
    this.login_flag();
  },
  // 倒计时
  countDown: function () {
    var that = this;
    var countDownNum = that.data.countDownNum;
    that.setData({
      timer: setInterval(function () {
        if (countDownNum == 0) {
          wx.clearStorage("code_time")
          clearInterval(that.data.timer);
          that.setData({
            setcode: false,
            countDownNum: 60
          });
          return;
        } else {
          countDownNum--;
          that.setData({
            countDownNum: countDownNum
          })
        }
        wx.setStorage({
          key: 'code_time',
          data: that.data.countDownNum
        })
      }, 1000),
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 倒计时缓存
    var that=this;
    wx.getStorage({
      key: 'code_time',
      success(res) {
        that.setData({
          countDownNum: res.data,
          setcode: true
        })
        that.countDown();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
   //注册协议
   agreement: function (e) {
    var webUrl = "/Api/Article/agreement/doc_code/agreement";
    this.setData({ webUrl: webUrl });
    wx.navigateTo({ url: '/pages/index/webview/webview' });
  },
})